package vip.yuange.gsyzm.utils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

import javax.imageio.ImageIO;

public class PublicUtils {
	
	public static BufferedImage base64ToBufferedImage(String base64Image) throws IOException {
        byte[] imageBytes = Base64.getDecoder().decode(base64Image); 
        ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
        BufferedImage image = ImageIO.read(bais); 
        bais.close();
        return image;  
    }

	public static String gsyzmColor(String gsyzmColor) {
		if(gsyzmColor.contains("红色")) {
			return "RED";
		}else if(gsyzmColor.contains("蓝色")) {
			return "BLUE";
		}else if(gsyzmColor.contains("黄色")) {
			return "YELLOW";
		}else if(gsyzmColor.contains("绿色")){
			return "GREEN";
		}else {
			return "BLACK";
		}
	}
	
	public static BufferedImage keepColorAndMakeOthersWhite(BufferedImage img, String colorName,int tolerance) throws IOException {  
		BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);  
		int targetColor = getColorValue(colorName);  
		if (targetColor == -1) {  
			return null;  
		}  
		for (int x = 0; x < img.getWidth(); x++) {  
			for (int y = 0; y < img.getHeight(); y++) {  
				int pixel = img.getRGB(x, y); 
				if (isColorMatchInRange(pixel, targetColor,tolerance)) {  
					result.setRGB(x, y, pixel);  
				} else {  
					result.setRGB(x, y, 0xFFFFFFFF);   
				}  
			}  
		}  
		return result;  
	} 
	
	private static int getColorValue(String colorName) {  
		switch (colorName.toUpperCase()) {  
		case "YELLOW":  
			return new Color(255, 255, 0).getRGB() & 0xFFFFFF; 
		case "RED":  
			return new Color(255, 0, 0).getRGB() & 0xFFFFFF;  
		case "BLUE":  
			return new Color(0, 0, 255).getRGB() & 0xFFFFFF;  
		case "GREEN":  
			return new Color(0, 255, 0).getRGB() & 0xFFFFFF;  
		case "BLACK":  
			return new Color(0, 0, 0).getRGB() & 0xFFFFFF; 
		default:  
			return -1;  
		}  
	}  

	private static boolean isColorMatchInRange(int pixel, int targetColor, int tolerance) {  
		int pixelRGB = pixel & 0xFFFFFF;  
		int targetRGB = targetColor & 0xFFFFFF;  
		int rPixel = (pixelRGB >> 16) & 0xFF;  
		int gPixel = (pixelRGB >> 8) & 0xFF;  
		int bPixel = pixelRGB & 0xFF; 
		int rTarget = (targetRGB >> 16) & 0xFF;  
		int gTarget = (targetRGB >> 8) & 0xFF;  
		int bTarget = targetRGB & 0xFF;    
		int diff = Math.abs(rPixel - rTarget) + Math.abs(gPixel - gTarget) + Math.abs(bPixel - bTarget);  
		return diff <= tolerance;  
	}  
	
    public static BufferedImage reSize(BufferedImage image, int width, int height) {
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image tmp = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        Graphics2D g2d = newImage.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();
        return newImage;
    }

    public static BufferedImage getGray(BufferedImage image) {
        BufferedImage target = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g2d = target.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();
        return target;
    }
    
    public static String bufferedImageToBase64(BufferedImage bufferedImage) {  
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {  
			ImageIO.write(bufferedImage, "png", baos);  
			byte[] bytes = baos.toByteArray();  
			String base64Encoded = Base64.getEncoder().encodeToString(bytes);  
			return base64Encoded;  
		} catch (IOException e) {  
			e.printStackTrace();  
			return null;
		}  
	}  
    
}
